home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / doc / string.doc < prev    next >
Text File  |  1993-07-05  |  30KB  |  1,415 lines

  1.  
  2.     STRING.DOC (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  3.  
  4. TABLE OF CONTENTS
  5.  
  6. c.lib/string/stpcpy
  7. c.lib/string/strbpl
  8. c.lib/string/strcat
  9. c.lib/string/strchr
  10. c.lib/string/strcmp
  11. c.lib/string/strcpy
  12. c.lib/string/strcspn
  13. c.lib/string/strdup
  14. c.lib/string/strerror
  15. c.lib/string/stricmp
  16. c.lib/string/strins
  17. c.lib/string/strlen
  18. c.lib/string/strncat
  19. c.lib/string/strncmp
  20. c.lib/string/strncpy
  21. c.lib/string/strnicmp
  22. c.lib/string/strpbrk
  23. c.lib/string/strrchr
  24. c.lib/string/strspn
  25. c.lib/string/strstr
  26. c.lib/string/strtod
  27. c.lib/string/strtok
  28. c.lib/string/strtol
  29.  
  30.  
  31. string/stpcpy                        string/stpcpy
  32.  
  33.    NAME
  34.     stpcpy    - copy a string returning a pointer to the end of the destination
  35.  
  36.    SYNOPSIS
  37.     char *ptr = stpcpy(d, s);
  38.     char *d;
  39.     char *s;
  40.  
  41.    FUNCTION
  42.     Copy the nul terminated string pointed to by s to the buffer d.
  43.     The nul is copied.  A pointer to the nul character at the end
  44.     of the copied string in d is returned.
  45.  
  46.    NOTE
  47.     stpcpy() is a non-standard function.  While a stpcpy()/stpcpy()
  48.     combination is more efficient than a strcpy()/strcat() combination,
  49.     strcpy() and strcat() are standard functions and thus guarenteed
  50.     to exist in all enviroments.
  51.  
  52.    EXAMPLE
  53.     #include <stdio.h>
  54.     #include <string.h>
  55.     #include <assert.h>
  56.  
  57.     main()
  58.     {
  59.         char *buf1 = "hello";
  60.         char *buf2 = "123";
  61.         char dest[32];
  62.         char *ptr;
  63.  
  64.         ptr = stpcpy(dest, buf1);
  65.         stpcpy(ptr,  buf2);
  66.         puts(dest);                     /* hello123 */
  67.         return(0);
  68.     }
  69.  
  70.    INPUTS
  71.     char *d;    pointer to beginning of destination buffer
  72.     char *s;    pointer to beginning of source string
  73.  
  74.    RESULTS
  75.     char *ptr;    pointer to end of data copied to destination buffer
  76.  
  77.    SEE ALSO
  78.     strcpy
  79.  
  80.  
  81. string/strbpl                        string/strbpl
  82.  
  83.    NAME
  84.     strbpl    - unpack a string-array buffer into an array of pointers
  85.  
  86.    SYNOPSIS
  87.     int num = strbpl(av, max, sary)
  88.     char **av;
  89.     int max;
  90.     const char *sary;
  91.  
  92.    FUNCTION
  93.     Unpacks a string-array into an array of string pointers.  The
  94.     string array is a series of nul terminated strings strung together
  95.     and terminated by a final nul.    A pointer to each string is placed
  96.     in the arary-of-pointers (av) with a final NULL entry assuming
  97.     the number of strings does not exceed (max-1)
  98.  
  99.    EXAMPLE
  100.     #include <stdio.h>
  101.     #include <string.h>
  102.     #include <assert.h>
  103.  
  104.     main()
  105.     {
  106.         char *sary = "this\0is\0a\0test\0\0";
  107.         char *av[16];
  108.         int n;
  109.  
  110.         #define arysize(x)  (sizeof(x)/sizeof((x)[0]))
  111.  
  112.         n = strbpl(av, arysize(av), sary);
  113.         assert(n == 4);                     /*  n == 4          */
  114.         puts(av[0]);                        /*  this            */
  115.         puts(av[1]);                        /*  is              */
  116.         puts(av[2]);                        /*  a               */
  117.         puts(av[3]);                        /*  test            */
  118.         assert(av[4] == NULL);              /*  av[4] == NULL   */
  119.         return(0);
  120.     }
  121.  
  122.    INPUTS
  123.     char **av;    pointer to a preallocated array of pointers
  124.     int max;    the maximum number of entries in the above array
  125.     char *sary;    pointer to a packed string.
  126.  
  127.    RESULTS
  128.     int num;    number of pointers loaded into the av array not
  129.             including the final NULL.  If num == max then
  130.             the av array was not large enough to fit all
  131.             the strings or the final NULL.
  132.  
  133.    SEE ALSO
  134.  
  135.  
  136.  
  137. string/strcat                        string/strcat
  138.  
  139.    NAME
  140.     strcat    - concactenate a string to an existing string
  141.  
  142.    SYNOPSIS
  143.     char *d = strcat(d, s);
  144.     char *d;
  145.     const char *s;
  146.  
  147.    FUNCTION
  148.     scans the destination buffer for the nul terminator and then
  149.     appends the source string to the destination buffer (removing
  150.     the nul terminator and placing one at the end after the
  151.     concactenation).
  152.  
  153.     A pointer to the beginning of the destination buffer is returned.
  154.  
  155.    EXAMPLE
  156.     #include <stdio.h>
  157.     #include <string.h>
  158.     #include <assert.h>
  159.  
  160.     main()
  161.     {
  162.         char d[32];
  163.         char *s1 = "fu";
  164.         char *s2 = "bar";
  165.         char *p;
  166.  
  167.         strcpy(d, s1);
  168.         p = strcat(d, s2);
  169.  
  170.         assert(p == d);         /*  strcat returns its first argument */
  171.         puts(d);                /*  fubar       */
  172.         return(0);
  173.     }
  174.  
  175.    INPUTS
  176.     char *d;    pointer to destination buffer which already contains
  177.             a string (which could be just a \0).
  178.  
  179.     char *s;    pointer to the nul terminated source string
  180.  
  181.    RESULTS
  182.     char *d;    same as the first argument, a pointer to the
  183.             destination buffer.
  184.  
  185.    SEE ALSO
  186.     strncpy, strcpy, strncat
  187.  
  188.  
  189. string/strchr                        string/strchr
  190.  
  191.    NAME
  192.     strchr    - search for a character in a string
  193.  
  194.    SYNOPSIS
  195.     char *ptr = strchr(s, c)
  196.     const char *s;
  197.     int c;
  198.  
  199.    FUNCTION
  200.     Searches for the character c within the string pointed to by
  201.     s.  The terminating nul at the end of s is included in the search.
  202.  
  203.     A pointer to the first occurance of c in s is returned or NULL
  204.     if c could not be found.
  205.  
  206.     c is converted to a char by strchr() before beginning the search.
  207.  
  208.    NOTE
  209.     while strchr(s, 0); may be used to find the end of the string this
  210.     is slow compared to using the construction:
  211.  
  212.         char *ptr = s + strlen(s);    /*  ptr = end of string s       */
  213.  
  214.    EXAMPLE
  215.     #include <stdio.h>
  216.     #include <string.h>
  217.     #include <assert.h>
  218.  
  219.     main()
  220.     {
  221.         char *s = "this is a test";
  222.         char *ptr;
  223.  
  224.         ptr = strchr(s, 'i');
  225.         assert(ptr == s + 2);
  226.  
  227.         puts(ptr);              /*  "is is a test"  */
  228.         return(0);
  229.     }
  230.  
  231.    INPUTS
  232.     char *s;    pointer to the string to search
  233.     int c;        character to search for
  234.  
  235.    RESULTS
  236.     char *ptr;    pointer to the first occurance of character c in
  237.             s or NULL if c could not be found in s.
  238.  
  239.    SEE ALSO
  240.     strrchr
  241.  
  242.  
  243. string/strcmp                        string/strcmp
  244.  
  245.    NAME
  246.     strcmp    - compare two strings
  247.  
  248.    SYNOPSIS
  249.     int r = strcmp(s1, s2);
  250.     const char *s1;
  251.     const char *s2;
  252.  
  253.    FUNCTION
  254.     Compares two strings, returning:
  255.         -1    s1 < s2
  256.          0    s1 == s2
  257.          1    s1 > s2
  258.  
  259.    NOTE
  260.     strcmp() converts the chars in the string to unsigned quantities
  261.     when comparing them.  However, for portability you should not
  262.     strcmp() strings containing negative characters (bit 7 set) for
  263.     anything other than checking the result against 0.  Use the
  264.     memcmp() routine instead.
  265.  
  266.    EXAMPLE
  267.     #include <stdio.h>
  268.     #include <string.h>
  269.     #include <assert.h>
  270.  
  271.     main()
  272.     {
  273.         char *s1 = "abca";
  274.         char *s2 = "abcd";
  275.         char *s3 = "abcx";
  276.         char *s4 = "abcdx";
  277.         char *s5 = "abc";
  278.         char *x2 = "abcd";
  279.         int r;
  280.  
  281.         r = strcmp(s2, x2);     /*  string s2 same as string x2 */
  282.         assert(r == 0);
  283.  
  284.         r = strcmp(s2, s1);     /*  string s2 larger than string s1 */
  285.         assert(r > 0);
  286.  
  287.         r = strcmp(s2, s3);
  288.         assert(r < 0);
  289.  
  290.         r = strcmp(s2, s4);
  291.         assert(r < 0);
  292.  
  293.         r = strcmp(s2, s5);
  294.         assert(r > 0);
  295.  
  296.         return(0);
  297.     }
  298.  
  299.    INPUTS
  300.     char *s1;    pointer to first string
  301.     char *s2;    pointer to second string
  302.  
  303.    RESULTS
  304.     int r;        result: -1, 0, or 1.
  305.  
  306.    SEE ALSO
  307.     strncmp, stricmp
  308.  
  309.  
  310. string/strcpy                        string/strcpy
  311.  
  312.    NAME
  313.     strcpy    - copy a string returning a pointer to the beginning of the
  314.           destination.
  315.  
  316.    SYNOPSIS
  317.     char *ptr = strcpy(d, s);
  318.     char *d;
  319.     char *s;
  320.  
  321.    FUNCTION
  322.     Copy the nul terminated string pointed to by s to the buffer d.
  323.     The nul is copied.  The first argument is returned (a pointer to
  324.     the buffer d).
  325.  
  326.    EXAMPLE
  327.     #include <stdio.h>
  328.     #include <string.h>
  329.     #include <assert.h>
  330.  
  331.     /*
  332.      *  note that the stpcpy() example accomplishes the same thing and
  333.      *  is more efficient, but also requires the use of a temporary
  334.      *  pointer as well as cluttering the source and being non-standard.
  335.      *
  336.      *  strcpy()/strcat() is more portable though less efficient.
  337.      */
  338.  
  339.     main()
  340.     {
  341.         char *buf1 = "hello";
  342.         char *buf2 = "123";
  343.         char dest[32];
  344.         char *ptr;
  345.  
  346.         strcpy(dest, buf1);
  347.         strcat(dest, buf2);
  348.         puts(dest);                     /* hello123 */
  349.         return(0);
  350.     }
  351.  
  352.    INPUTS
  353.     char *d;    pointer to beginning of destination buffer
  354.     char *s;    pointer to beginning of source string
  355.  
  356.    RESULTS
  357.     char *ptr;    same as the destination buffer pointer (d).
  358.  
  359.    SEE ALSO
  360.     stpcpy
  361.  
  362.  
  363. string/strcspn                        string/strcspn
  364.  
  365.    NAME
  366.     strcspn - scan a string until a character is found that matches
  367.           any character in a second string.
  368.  
  369.    SYNOPSIS
  370.     int len = strcspn(s, toks)
  371.     const char *s;
  372.     const char *toks;
  373.  
  374.    FUNCTION
  375.     The string s is scanned until a character is found that matches any
  376.     character in the string toks.  The number of characters skipped is
  377.     returned.  If no character in s matches any character in toks then
  378.     the length of the string s is returned.
  379.  
  380.     This function is normally used to search for whitespace within a
  381.     string.  Note that in many cases strpbrk() is more useful than
  382.     strcspn().
  383.  
  384.    EXAMPLE
  385.     #include <stdio.h>
  386.     #include <string.h>
  387.     #include <assert.h>
  388.  
  389.     main()
  390.     {
  391.         int len;
  392.  
  393.         len = strcspn("hello this is a test", " \tabcd");
  394.         assert(len == 5);               /*  stopped at the first space  */
  395.  
  396.         len = strcspn("hello this is a test", " abl");
  397.         assert(len == 2);               /*  stopped at the first 'l'    */
  398.  
  399.         len = strcspn("hello", "abcd");
  400.         assert(len == 5);               /*  stopped at end of string 1  */
  401.  
  402.         return(0);
  403.     }
  404.  
  405.    INPUTS
  406.     char *s;    pointer to string to scan
  407.     char *toks;    pointer to string containing characters to compare
  408.             against
  409.  
  410.    RESULTS
  411.     int len;    # of characters skipped in s before a match was found
  412.  
  413.    SEE ALSO
  414.     strpbrk, strspn
  415.  
  416.  
  417. string/strdup                        string/strdup
  418.  
  419.    NAME
  420.     strdup    - duplicate a string using malloc
  421.  
  422.  
  423.    SYNOPSIS
  424.     char *s2 = strdup(s1);
  425.     const char *s1;
  426.  
  427.    FUNCTION
  428.     strdup malloc's enough space to hold s1 including the terminating
  429.     nul and then copies s1 into this space, returning a pointer to
  430.     the new string.  NULL is returned if space could not be allocated
  431.     due to low memory conditions.
  432.  
  433.     free() may be used to free the returned string.  The amount
  434.     malloc'd is (strlen(s1) + 1).
  435.  
  436.    NOTE
  437.     strdup() is a non-standard function and may not exist in other
  438.     C enviroments.
  439.  
  440.    EXAMPLE
  441.     #include <stdio.h>
  442.     #include <string.h>
  443.     #include <assert.h>
  444.  
  445.     /*
  446.      *  Modifying string constants (quoted strings) may not be entirely
  447.      *  portable.  Normally one does not use strdup() to accomplish the
  448.      *  following function but instead declares a char array statically
  449.      *  initialized with the string, such as:
  450.      *
  451.      *  char FuBar[] = { "This is a test" };
  452.      *
  453.      *  Which can be modified in a portable fashion without having to
  454.      *  duplicate the string.
  455.      */
  456.  
  457.     main()
  458.     {
  459.         char *s1 = "this is a test";
  460.         char *s2;
  461.  
  462.         s2 = strdup(s1);
  463.         s2[0] = 'x';
  464.         puts(s2);           /*  xhis is a test  */
  465.         free(s2);
  466.  
  467.         s2 = strdup(s1);
  468.         s2[1] = '0';
  469.         puts(s2);           /*  t0is is a test  */
  470.         free(s2);
  471.  
  472.         return(0);
  473.     }
  474.  
  475.    INPUTS
  476.     char *s1;    pointer to the string to duplicate
  477.  
  478.    RESULTS
  479.     char *s2;    pointer to malloc'd space containing a duplicate
  480.             of the string s1 or NULL if space could not be
  481.             malloc'd.
  482.  
  483.    SEE ALSO
  484.     malloc, free, strcpy, strlen
  485.  
  486.  
  487. string/strerror                     string/strerror
  488.  
  489.    NAME
  490.     strerror - return error string associated with error code
  491.  
  492.    SYNOPSIS
  493.     const char *str = strerror(error);
  494.     int error;
  495.  
  496.    FUNCTION
  497.     strerror returns a read-only string associated with the specified
  498.     error, usually taken from errno after some c.lib call fails.
  499.  
  500.     An unknown error will result in the string "unknown error"
  501.  
  502.    EXAMPLE
  503.     #include <stdio.h>
  504.     #include <errno.h>
  505.     #include <assert.h>
  506.  
  507.     main()
  508.     {
  509.         FILE *fi;
  510.  
  511.         fi = fopen("ThisFileDoesNotExist", "r");
  512.         assert(fi == NULL);
  513.         puts(strerror(errno));
  514.  
  515.         return(0);
  516.     }
  517.  
  518.    INPUTS
  519.     int error;    error code
  520.  
  521.    RESULTS
  522.     char *str;    error string
  523.  
  524.    SEE ALSO
  525.  
  526.  
  527. string/stricmp                        string/stricmp
  528.  
  529.    NAME
  530.     stricmp  - compare two strings, case insensitive
  531.  
  532.    SYNOPSIS
  533.     int r = stricmp(s1, s2);
  534.     const char *s1;
  535.     const char *s2;
  536.  
  537.    FUNCTION
  538.     Compares two strings, returning:
  539.         -1    s1 < s2
  540.          0    s1 == s2
  541.          1    s1 > s2
  542.  
  543.     stricmp differs from strcmp in that case is ignored for alphabetic
  544.     characters.  i.e. a == A
  545.  
  546.    NOTE
  547.     stricmp() converts the chars in the string to unsigned quantities
  548.     when comparing them.  However, for portability you should not
  549.     stricmp() strings containing negative characters (bit 7 set) for
  550.     anything other than checking the result against 0.  Use the
  551.     memcmp() routine instead.
  552.  
  553.    EXAMPLE
  554.     #include <stdio.h>
  555.     #include <string.h>
  556.     #include <assert.h>
  557.  
  558.     main()
  559.     {
  560.         char *s1 = "abCa";
  561.         char *s2 = "aBcD";
  562.         char *s3 = "aBCX";
  563.         char *s4 = "ABCdx";
  564.         char *s5 = "Abc";
  565.         char *x2 = "ABCD";
  566.         int r;
  567.  
  568.         r = stricmp(s2, x2);     /*  string s2 same as string x2 */
  569.         assert(r == 0);
  570.  
  571.         r = stricmp(s2, s1);     /*  string s2 larger than string s1 */
  572.         assert(r > 0);
  573.  
  574.         r = stricmp(s2, s3);
  575.         assert(r < 0);
  576.  
  577.         r = stricmp(s2, s4);
  578.         assert(r < 0);
  579.  
  580.         r = stricmp(s2, s5);
  581.         assert(r > 0);
  582.  
  583.         return(0);
  584.     }
  585.  
  586.    INPUTS
  587.     char *s1;    pointer to first string
  588.     char *s2;    pointer to second string
  589.  
  590.    RESULTS
  591.     int r;        result: -1, 0, or 1.
  592.  
  593.    SEE ALSO
  594.     strcmp, strncmp
  595.  
  596.  
  597.  
  598. string/strins                        string/strins
  599.  
  600.    NAME
  601.     strins - insert one string within another
  602.  
  603.    SYNOPSIS
  604.     void strins(d, s);
  605.     char *d;
  606.     const char *s;
  607.  
  608.    FUNCTION
  609.     strins inserts string s into d by shifting the string in d over
  610.     strlen(s) spaces and then copying s into the newly made hold (except
  611.     for the nul, of course).  This result is s inserted into d.
  612.  
  613.    NOTE
  614.     There must be enough room in d to insert s.  i.e. if d is an array
  615.     of 32 chars and contains a string of 8 chars you can insert another
  616.     string of, say, 10 chars, but not of 30 chars.
  617.  
  618.     strins is NOT an ANSI standard function
  619.  
  620.    EXAMPLE
  621.     #include <stdio.h>
  622.     #include <string.h>
  623.  
  624.     main()
  625.     {
  626.         char buf[32];
  627.  
  628.         strcpy(buf, "This is a test");
  629.         strins(buf + 5, "<gak!> ");
  630.         puts(buf);      /*  This <gak!> is a test   */
  631.  
  632.         return(0);
  633.     }
  634.  
  635.    INPUTS
  636.     char *d;        destination to insert in front of
  637.     char *s;        source string to insert
  638.  
  639.    RESULTS
  640.     none
  641.  
  642.    SEE ALSO
  643.     strcpy, strcat, strlen
  644.  
  645.  
  646. string/strlen                        string/strlen
  647.  
  648.    NAME
  649.     strlen - returns length of a string
  650.  
  651.    SYNOPSIS
  652.     int len = strlen(s);
  653.     const char *s;
  654.  
  655.    FUNCTION
  656.     The length of the requested string is returned.  The string is
  657.     scanned until a nul terminator is found and the number of
  658.     characters (not including the nul) is returned.
  659.  
  660.    EXAMPLE
  661.     #include <stdio.h>
  662.     #include <string.h>
  663.     #include <assert.h>
  664.  
  665.     main()
  666.     {
  667.         char buf[32];
  668.         int len;
  669.  
  670.         strcpy(buf, "Fu Bar Bear Boo");
  671.         len = strlen(buf);
  672.  
  673.         assert(len == 15);
  674.  
  675.         strcat(buf, "xx");
  676.  
  677.         len = strlen(buf);
  678.  
  679.         assert(len == 17);
  680.  
  681.         return(0);
  682.     }
  683.  
  684.    INPUTS
  685.     char *s;        string to obtain length of
  686.  
  687.    RESULTS
  688.     int len;        length of string
  689.  
  690.    SEE ALSO
  691.     strcpy, strcat
  692.  
  693.  
  694. string/strncat                        string/strncat
  695.  
  696.    NAME
  697.     strncat  - concactenate a string to an existing string up to a
  698.            maximum number of characters
  699.  
  700.    SYNOPSIS
  701.     char *d = strncat(d, s, n);
  702.     char *d;
  703.     const char *s;
  704.     int n;
  705.  
  706.    FUNCTION
  707.     scans the destination buffer for the nul terminator and then
  708.     appends the source string to the destination buffer (removing
  709.     the nul terminator and placing one at the end after the
  710.     concactenation).  However, only up to n characters is concactenated
  711.     including the nul.  If the source string is exactly n characters
  712.     long no nul will be appended.  If the source string is longer
  713.     than n characters then only the first n characters of the source
  714.     string will be appended (and no nul will be).
  715.  
  716.     A pointer to the beginning of the destination buffer is returned.
  717.  
  718.    EXAMPLE
  719.     #include <stdio.h>
  720.     #include <string.h>
  721.     #include <assert.h>
  722.  
  723.     main()
  724.     {
  725.         char d[32];
  726.         char *s1 = "fu";
  727.         char *s2 = "bar";
  728.         char *p;
  729.  
  730.         d[2] = 23;
  731.         d[5] = 24;
  732.         d[6] = 25;
  733.         strcpy(d, s1);          /*  overwrites d[2] with a nul  */
  734.         assert(d[2] == 0);
  735.  
  736.         p = strncat(d, s2, 3);  /*  does NOT overwrite d[5] with a nul */
  737.         assert(d[5] == 24);
  738.         assert(p == d);
  739.  
  740.         strcpy(d, s1);
  741.         p = strncat(d, s2, 20); /*  does        */
  742.         assert(p == d);
  743.         assert(d[5] == 0);
  744.         assert(d[6] == 25);     /*  stops at the nul, so d[6] was not modified */
  745.  
  746.         puts(d);                /*  fubar   */
  747.  
  748.         return(0);
  749.     }
  750.  
  751.    INPUTS
  752.     char *d;    pointer to destination buffer which already contains
  753.             a string (which could be just a \0).
  754.  
  755.     char *s;    pointer to the nul terminated source string
  756.  
  757.     int n;        maximum number of characters to concactenate
  758.  
  759.    RESULTS
  760.     char *d;    same as the first argument, a pointer to the
  761.             destination buffer.
  762.  
  763.    SEE ALSO
  764.     strncpy, strcpy, strcat
  765.  
  766.  
  767. string/strncmp                        string/strncmp
  768.  
  769.    NAME
  770.     strncmp  - compare two strings up to a maximum number of characters
  771.  
  772.    SYNOPSIS
  773.     int r = strncmp(s1, s2, n);
  774.     const char *s1;
  775.     const char *s2;
  776.     int n;
  777.  
  778.    FUNCTION
  779.     Compares two strings, returning:
  780.         -1    s1 < s2
  781.          0    s1 == s2
  782.          1    s1 > s2
  783.  
  784.     strncmp() works like strcmp() but only up to n characters will be
  785.     compared.  If all characters compare when n is reached 0 is
  786.     returned indicating that the strings matched.  Fewer characters
  787.     might be compared if either string terminates (w/ a nul) before
  788.     the maximum is reached or a compare fails (scan is stopped and -1
  789.     or 1 is returned immediately)
  790.  
  791.    NOTE
  792.     strncmp() converts the chars in the string to unsigned quantities
  793.     when comparing them.  However, for portability you should not
  794.     strncmp() strings containing negative characters (bit 7 set) for
  795.     anything other than checking the result against 0.  Use the
  796.     memcmp() routine instead.
  797.  
  798.    EXAMPLE
  799.     #include <stdio.h>
  800.     #include <string.h>
  801.     #include <assert.h>
  802.  
  803.     main()
  804.     {
  805.         char *s1 = "abcaq";
  806.         char *s2 = "abcdr";
  807.         char *s3 = "abcxs";
  808.         char *s4 = "abcdxx";
  809.         char *s5 = "abc";
  810.         char *x2 = "abcdt";
  811.         int r;
  812.  
  813.         r = strncmp(s2, x2, 4);
  814.         assert(r == 0);
  815.  
  816.         r = strncmp(s2, s1, 4);
  817.         assert(r > 0);
  818.  
  819.         r = strncmp(s2, s3, 4);
  820.         assert(r < 0);
  821.  
  822.         r = strncmp(s2, s4, 8);
  823.         assert(r < 0);
  824.  
  825.         r = strncmp(s2, s5, 4);
  826.         assert(r > 0);
  827.  
  828.         return(0);
  829.     }
  830.  
  831.    INPUTS
  832.     char *s1;    pointer to first string
  833.     char *s2;    pointer to second string
  834.     int n;        maximum number of characters to compare
  835.  
  836.    RESULTS
  837.     int r;        result: -1, 0, or 1.
  838.  
  839.    SEE ALSO
  840.     strncmp, stricmp
  841.  
  842.  
  843. string/strncpy                        string/strncpy
  844.  
  845.    NAME
  846.     strncpy - copy a string returning a pointer to the beginning of the
  847.           destination until nul or the specified number of characters
  848.           is reached.
  849.  
  850.    SYNOPSIS
  851.     char *ptr = strncpy(d, s, n);
  852.     char *d;
  853.     const char *s;
  854.     int n;
  855.  
  856.    FUNCTION
  857.     Copy the nul terminated string pointed to by s to the buffer d. The
  858.     nul is normally copied.  The first argument is returned (a pointer
  859.     to the buffer d).
  860.  
  861.     The copy will also be terminated if the specified maximum is reached,
  862.     in which case the nul is NOT copied.
  863.  
  864.    EXAMPLE
  865.     #include <stdio.h>
  866.     #include <string.h>
  867.     #include <assert.h>
  868.  
  869.     /*
  870.      *  This is a dumb example
  871.      */
  872.  
  873.     main()
  874.     {
  875.         char *buf1 = "hello";
  876.         char *buf2 = "123";
  877.         char dest[32];
  878.         char *ptr;
  879.  
  880.         strncpy(dest, buf1, 8);
  881.         strcat(dest, buf2);
  882.         puts(dest);                     /* hello123 */
  883.  
  884.         dest[2] = 23;
  885.         strncpy(dest, buf1, 2);
  886.         assert(dest[2] == 23);          /* it only copied to chars!     */
  887.  
  888.         dest[2] = 0;            /*    note we have to add the nul */
  889.         strcat(dest, buf2);             /*  he123   */
  890.         puts(dest);
  891.  
  892.         return(0);
  893.     }
  894.  
  895.    INPUTS
  896.     char *d;    pointer to beginning of destination buffer
  897.     char *s;    pointer to beginning of source string
  898.     int len;    maximum number of characters to copy
  899.  
  900.    RESULTS
  901.     char *ptr;    same as the destination buffer pointer (d).
  902.  
  903.    SEE ALSO
  904.     stpcpy, strcpy
  905.  
  906.  
  907. string/strnicmp                     string/strnicmp
  908.  
  909.    NAME
  910.     strnicmp  - compare two strings up to a maximum number of characters
  911.             and case insensitive
  912.  
  913.    SYNOPSIS
  914.     int r = strnicmp(s1, s2, n);
  915.     const char *s1;
  916.     const char *s2;
  917.     int n;
  918.  
  919.    FUNCTION
  920.     Compares two strings, returning:
  921.         -1    s1 < s2
  922.          0    s1 == s2
  923.          1    s1 > s2
  924.  
  925.     strnicmp differs from strcmp in that case is ignored for alphabetic
  926.     characters, i.e. a == A, and only up to n characters are compared.
  927.  
  928.     Refer to stricmp() and strncmp() for other examples
  929.  
  930.    NOTE
  931.     strnicmp() converts the chars in the string to unsigned quantities
  932.     when comparing them.  However, for portability you should not
  933.     strnicmp() strings containing negative characters (bit 7 set) for
  934.     anything other than checking the result against 0.  Use the
  935.     memcmp() routine instead.
  936.  
  937.    EXAMPLE
  938.     #include <stdio.h>
  939.     #include <string.h>
  940.     #include <assert.h>
  941.  
  942.     main()
  943.     {
  944.         char *s1 = "aBcAQ";
  945.         char *s2 = "abCDR";
  946.         char *s3 = "ABcXs";
  947.         char *s4 = "aBCDxX";
  948.         char *s5 = "aBC";
  949.         char *x2 = "AbCDt";
  950.         int r;
  951.  
  952.         r = strnicmp(s2, x2, 4);
  953.         assert(r == 0);
  954.  
  955.         r = strnicmp(s2, s1, 4);
  956.         assert(r > 0);
  957.  
  958.         r = strnicmp(s2, s3, 4);
  959.         assert(r < 0);
  960.  
  961.         r = strnicmp(s2, s4, 8);
  962.         assert(r < 0);
  963.  
  964.         r = strnicmp(s2, s5, 4);
  965.         assert(r > 0);
  966.  
  967.         return(0);
  968.     }
  969.  
  970.    INPUTS
  971.     char *s1;    pointer to first string
  972.     char *s2;    pointer to second string
  973.     int n;        maximum # of characters to compare
  974.  
  975.    RESULTS
  976.     int r;        result: -1, 0, or 1.
  977.  
  978.    SEE ALSO
  979.     strcmp, strncmp, stricmp
  980.  
  981.  
  982. string/strpbrk                        string/strpbrk
  983.  
  984.    NAME
  985.     strpbrk - search for specific character(s) (tokens) in string
  986.  
  987.    SYNOPSIS
  988.     char *ptr = strpbrk(s, toks)
  989.     const char *s;
  990.     char *toks;
  991.  
  992.    FUNCTION
  993.     Searches the string s for any character in the string toks.  For
  994.     example, when searching for whitespace in s, toks would contain the
  995.     space and tab character.
  996.  
  997.     If no character in s matches any character in toks then NULL is
  998.     returned.
  999.  
  1000.    EXAMPLE
  1001.     #include <stdio.h>
  1002.     #include <string.h>
  1003.     #include <assert.h>
  1004.  
  1005.     main()
  1006.     {
  1007.         char *s = "This  \tis a test";
  1008.         char *ptr;
  1009.  
  1010.         ptr = strpbrk(s, " \t");
  1011.         assert(ptr == s + 4);
  1012.  
  1013.         ptr = strpbrk(ptr + 1, " \t");
  1014.         assert(ptr == s + 5);
  1015.  
  1016.         ptr = strpbrk(ptr + 1, " \t");
  1017.         assert(ptr == s + 6);
  1018.  
  1019.         ptr = strpbrk(ptr + 1, " \t");
  1020.         assert(ptr == s + 9);
  1021.  
  1022.         ptr = strpbrk(ptr + 1, "xyz");  /*  doesn't find 'm */
  1023.         assert(ptr == NULL);
  1024.  
  1025.         return(0);
  1026.     }
  1027.  
  1028.    INPUTS
  1029.     char *s;    pointer to string to scan
  1030.     char *toks;    pointer to string containing tokens to scan for
  1031.  
  1032.    RESULTS
  1033.     char *ptr;    pointer to point in s where the character matches
  1034.             any character in toks, or NULL if s was exhausted.
  1035.  
  1036.    SEE ALSO
  1037.     strtok
  1038.  
  1039.  
  1040. string/strrchr                        string/strrchr
  1041.  
  1042.    NAME
  1043.     strrchr - search for a character in a string, scan backwards
  1044.  
  1045.    SYNOPSIS
  1046.     char *ptr = strrchr(s, c)
  1047.     const char *s;
  1048.     int c;
  1049.  
  1050.    FUNCTION
  1051.     Searches for the character c within the string pointed to by
  1052.     s.  The terminating nul at the end of s is included in the search.
  1053.     The string is searched backwards
  1054.  
  1055.     A pointer to the LAST occurance of c in s is returned or NULL
  1056.     if c could not be found.
  1057.  
  1058.     c is converted to an 8 bit quantity by strrchr().
  1059.  
  1060.    NOTE
  1061.     The ANSI spec does not say anything about including the nul
  1062.     character in the search for strrchr and some implementation
  1063.     may thus not implement this properly.
  1064.  
  1065.    EXAMPLE
  1066.     #include <stdio.h>
  1067.     #include <string.h>
  1068.     #include <assert.h>
  1069.  
  1070.     main()
  1071.     {
  1072.         char *s = "this is a test";
  1073.         char *ptr;
  1074.  
  1075.         ptr = strrchr(s, 'i');
  1076.         assert(ptr == s + 5);
  1077.  
  1078.         puts(ptr);              /*  "is a test"  */
  1079.  
  1080.         ptr = strrchr(s, 'x');
  1081.         assert(ptr == NULL);
  1082.  
  1083.         return(0);
  1084.     }
  1085.  
  1086.    INPUTS
  1087.     char *s;    pointer to the string to search
  1088.     int c;        character to search for
  1089.  
  1090.    RESULTS
  1091.     char *ptr;    pointer to the last occurance of character c in
  1092.             s or NULL if c could not be found in s.
  1093.  
  1094.    SEE ALSO
  1095.     strchr
  1096.  
  1097.  
  1098. string/strspn                        string/strspn
  1099.  
  1100.    NAME
  1101.     strspn    - scan a string until a character is found that does NOT match
  1102.           some character in a second string.
  1103.  
  1104.    SYNOPSIS
  1105.     int len = strspn(s, toks)
  1106.     const char *s;
  1107.     const char *toks;
  1108.  
  1109.    FUNCTION
  1110.     The string s is scanned until a character is found that does NOT
  1111.     match any character in the string toks.  The number of characters
  1112.     skipped is returned.  If every character in s matches some
  1113.     character in toks then the length of the string s is returned.
  1114.  
  1115.     This function is normally used to skip whitespace within a
  1116.     string.
  1117.  
  1118.    EXAMPLE
  1119.     #include <stdio.h>
  1120.     #include <string.h>
  1121.     #include <assert.h>
  1122.  
  1123.     main()
  1124.     {
  1125.         int len;
  1126.  
  1127.         len = strspn("  \t \t\t abcde test", " \t ");
  1128.         assert(len == 7);               /*  stopped at the 'a'  */
  1129.  
  1130.         len = strspn("abcd ", " ");
  1131.         assert(len == 0);
  1132.  
  1133.         len = strspn("   \t\t ", " \t");
  1134.         assert(len == 6);               /*  all match, len = strlen(str);   */
  1135.  
  1136.         return(0);
  1137.     }
  1138.  
  1139.    INPUTS
  1140.     char *s;    pointer to string to scan
  1141.     char *toks;    pointer to string containing characters to compare
  1142.             against
  1143.  
  1144.    RESULTS
  1145.     int len;    # of characters skipped in s before a match could not
  1146.             be found
  1147.  
  1148.    SEE ALSO
  1149.     strpbrk, strcspn
  1150.  
  1151.  
  1152. string/strstr                        string/strstr
  1153.  
  1154.    NAME
  1155.     strstr    - find sub string within another string
  1156.  
  1157.  
  1158.    SYNOPSIS
  1159.     char *ptr = strstr(s, sub);
  1160.     const char *s;
  1161.     const char *sub;
  1162.  
  1163.    FUNCTION
  1164.     The string s is scanned until the sub string sub matches the string
  1165.     beginning at the current scan point, and a pointer to the sub string
  1166.     within s is returned.  If the sub string could not be found NULL
  1167.     is returned.
  1168.  
  1169.    EXAMPLE
  1170.     #include <stdio.h>
  1171.     #include <string.h>
  1172.     #include <assert.h>
  1173.  
  1174.     main()
  1175.     {
  1176.         char *s = "abcdefghijklmnopqrstuvwxyz";
  1177.         char *ptr;
  1178.  
  1179.         ptr = strstr(s, "klm");
  1180.         assert(ptr == s + 10);
  1181.  
  1182.         puts(ptr);          /*  klmnopqrstuvwxyz    */
  1183.  
  1184.         return(0);
  1185.     }
  1186.  
  1187.    INPUTS
  1188.     char *s;    pointer to string to scan
  1189.     char *toks;    pointer to string containing characters to compare
  1190.             against
  1191.  
  1192.    RESULTS
  1193.     char *ptr;    point in s where sub string was found or NULL if
  1194.             sub string could not be found.
  1195.  
  1196.    SEE ALSO
  1197.     strpbrk, strcspn
  1198.  
  1199.  
  1200. string/strtod                        string/strtod
  1201.  
  1202.    NAME
  1203.     strtod    - Convert string to fp double
  1204.  
  1205.    SYNOPSIS
  1206.     double d = strtod(s, &tp);
  1207.     const char *s;
  1208.     char *tp;
  1209.  
  1210.    FUNCTION
  1211.     Converts a string to a floating point double.  Initial white space
  1212.     is skipped.  The format of the fp number in the string is then:
  1213.  
  1214.     {+/-/<nothing>}ddddd[.ddddd][E{+/-/<nothing>}dddd]
  1215.  
  1216.     Example fp strings: " +1.234E-3", "-1234", "6.5676E4", "214.2345"
  1217.  
  1218.    EXAMPLE
  1219.  
  1220.     /*
  1221.      *  compile -lm to include math library
  1222.      */
  1223.  
  1224.     #include <stdio.h>
  1225.     #include <string.h>
  1226.  
  1227.     main()
  1228.     {
  1229.         double d;
  1230.         char *tp;
  1231.  
  1232.         d = strtod("1.2134 3.45E2", &tp);
  1233.         printf("1.2134 = %lf\n", d);    /*  1.213400    */
  1234.  
  1235.         d = strtod(tp, &tp);
  1236.         printf("3.45E2 = %lf\n", d);    /*  345.000000  */
  1237.  
  1238.         return(0);
  1239.     }
  1240.  
  1241.    INPUTS
  1242.     char *s;    pointer to string containing fp number
  1243.  
  1244.     char **tp;    pointer to pointer, the pointer is modified to
  1245.             point to the end of the scanned fp number
  1246.  
  1247.    RESULTS
  1248.     double d;    resulting double
  1249.  
  1250.    SEE ALSO
  1251.  
  1252.  
  1253. string/strtok                        string/strtok
  1254.  
  1255.    NAME
  1256.     strtok    - Break up a string into arguments
  1257.  
  1258.    SYNOPSIS
  1259.     char *arg = strtok(s, toks)
  1260.     char *s;
  1261.     const char *toks;
  1262.  
  1263.    FUNCTION
  1264.     strtok() breaks up a string into arguments.  It determines the break
  1265.     point from the 'toks' string which contains a set of white space
  1266.     characters (usually " \t" to mean space and tab).
  1267.  
  1268.     The first call to strtok() should specify the string s and toks.
  1269.     Initial white space is skipped and the string is then scanned
  1270.     until the end of the first argument is found.  The string is
  1271.     then MODIFIED... a nul is placed at the end of the first argument
  1272.     and a pointer to the beginning of the first argument is returned.
  1273.  
  1274.     Further calls to strtok() should pass a NULL for the string s,
  1275.     which tells strtok() to continue scanning the original string (whos
  1276.     pointer was stored in a static char * within strtok).
  1277.  
  1278.     strtok() returns arguments until the string is exhausted, in which
  1279.     case is returns NULL.  The initial call to strtok() can return NULL
  1280.     if the passed string s contains nothing but whitespace (as specified
  1281.     by toks).
  1282.  
  1283.     You can change the toks string at any time (i.e. pass a different
  1284.     toks string to strtok).
  1285.  
  1286.    WARNING
  1287.     strtok modifies the source string and returns pointers into it.
  1288.  
  1289.    EXAMPLE
  1290.     #include <stdio.h>
  1291.     #include <string.h>
  1292.  
  1293.     main()
  1294.     {
  1295.         char buf[32];
  1296.         char *arg;
  1297.         const char *ws = " \t";
  1298.  
  1299.         /*
  1300.          *    'This' 'is' 'a' 'test!'
  1301.          */
  1302.  
  1303.         strcpy(buf, "  This  is \t \t a test!");
  1304.         for (arg = strtok(buf, ws); arg; arg = strtok(NULL, ws)) {
  1305.         printf("arg = '%s'\n", arg);
  1306.         }
  1307.         return(0);
  1308.     }
  1309.  
  1310.    INPUTS
  1311.     char *s;    pointer to string to parse
  1312.     char *toks;    pointer to string containing whitespace characters
  1313.             (argument delimiters)
  1314.  
  1315.    RESULTS
  1316.     char *arg;    pointer into s to next argument that is nul
  1317.             terminated (s is modified).
  1318.  
  1319.    SEE ALSO
  1320.     strspn, strcspn
  1321.  
  1322.  
  1323. string/strtol                        string/strtol
  1324.  
  1325.    NAME
  1326.     strtol    - Convert string to integer
  1327.  
  1328.    SYNOPSIS
  1329.     long v = strtol(str, &tail, base);
  1330.     const char *str;
  1331.     char *tail;
  1332.     int base;
  1333.  
  1334.    FUNCTION
  1335.     strtol() converts a string into an integer using the specified
  1336.     base 0-36.  If a non-zero base is specified conversion is done
  1337.     using that base (hex numbers may still be preceeded by '0x' or
  1338.     '0X').  If 0 is specified for the base then the base is
  1339.     determined from the first one or two characters of the number
  1340.     portion of the string:
  1341.  
  1342.         0        octal
  1343.         1-9     decimal
  1344.         0x        hex (0x or 0X)
  1345.  
  1346.     For bases larger than 10, alphabetic characters are used to
  1347.     represent digits.  Either lower case or upper case letters
  1348.     may be used.
  1349.  
  1350.     strtol() stores a pointer to the remainder of the string
  1351.     after the conversion.  strtol() ignores any whitespace at
  1352.     the beginning of the string and also handles an optional
  1353.     negative sign (which may preceed the numerical portion of
  1354.     the string).
  1355.  
  1356.     strtol() returns the converted value as a long, 0 if
  1357.     it was unable to convert anything, and an undefined result
  1358.     if the converted value is out of range.
  1359.  
  1360.    NOTE
  1361.     strtol() superceeds atoi() and atol().
  1362.  
  1363.    EXAMPLE
  1364.     #include <stdio.h>
  1365.     #include <string.h>
  1366.  
  1367.     main(ac, av)
  1368.     char *av[];
  1369.     {
  1370.         long v;
  1371.         char *tail;
  1372.  
  1373.         if (ac != 3) {
  1374.         puts("testprg <string> <base>");
  1375.         puts("testprg 0123abc 0");
  1376.         puts("testprg 0x1000 0");
  1377.         puts("testprg 0123abc 16");
  1378.         exit(1);
  1379.         }
  1380.         v = strtol(av[1], &tail, atoi(av[2]));
  1381.         printf("v = %d, tail = %s\n", v, tail);
  1382.         return(0);
  1383.     }
  1384.  
  1385.     1> testprg fffg 16
  1386.     v = 4095, tail = g
  1387.     1> testprg -0x100 0
  1388.     v = -256, tail =
  1389.     1> testprg 118 8
  1390.     v = 9, tail = 8
  1391.     1> testprg 11 0
  1392.     v = 11, tail =
  1393.     1> testprg 011xx 0
  1394.     v = 9, tail = xxx
  1395.  
  1396.  
  1397.    INPUTS
  1398.     char *str;        pointer to string to convert
  1399.  
  1400.     char **tail;        *tail modified to point to just
  1401.                 after last character converted
  1402.  
  1403.     int base;        base of conversion or 0 for
  1404.                 autoselect
  1405.  
  1406.    RESULTS
  1407.     long v;         converted result, an integer,
  1408.                 or 0 if no conversion could be
  1409.                 done.
  1410.  
  1411.    SEE ALSO
  1412.     atoi, atol
  1413.  
  1414.  
  1415.